package com.yz.xyt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.Major;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.vo.ClazzVo;
import com.yz.xyt.domain.vo.MajorImportVo;
import com.yz.xyt.domain.vo.MajorVo;
import com.yz.xyt.mapper.MajorMapper;
import com.yz.xyt.service.IClazzService;
import com.yz.xyt.service.IDepartmentService;
import com.yz.xyt.service.IMajorService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class MajorServiceImpl implements IMajorService {

    @Autowired
    MajorMapper majorMapper;

    @Autowired
    IDepartmentService departmentService;

    @Autowired
    IClazzService clazzService;

    @Override
    public int deleteByPrimaryKey(Integer sid) {
        return majorMapper.deleteByPrimaryKey(sid);
    }

    @Transactional
    @Override
    public int insert(Major record) {
        return majorMapper.insert(record);
    }

    @Transactional
    @Override
    public int insertSelective(Major record) {
        return majorMapper.insertSelective(record);
    }

    @Override
    public Major selectByPrimaryKey(Integer sid) {
        return majorMapper.selectByPrimaryKey(sid);
    }

    @Override
    public Major selectByMajorNumber(String majorNumber) {
        return majorMapper.selectByMajorNumber(majorNumber);
    }

    @Transactional
    @Override
    public int updateByPrimaryKeySelective(Major record) {
        return majorMapper.updateByPrimaryKeySelective(record);
    }

    @Transactional
    @Override
    public int updateByMajorNumberSelective(Major record) {
        return majorMapper.updateByMajorNumberSelective(record);
    }

    @Transactional
    @Override
    public int updateByPrimaryKey(Major record) {
        return majorMapper.updateByPrimaryKey(record);
    }

    @Transactional
    public void syncAddMajor(List<Major> list){
        //过滤重复数据
        List<Major> majorList = new ArrayList<>();
        list.stream().forEach(major -> {
            Major record = selectByMajorNumber(major.getMajorNumber());
            if(record == null){
                majorList.add(major);
            }
        });
        //新增专业信息
        majorList.stream().peek(major -> {
            major.setCreateBy(SecurityUtils.getUsername());
            major.setCreateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).stream().forEach(this::insertSelective);

    }

    /**
     * 同步更新
     * @param list
     */
    @Transactional
    @Override
    public void syncUpdateMajor(List<Major> list) {
        //更新班级信息
        list.stream().peek(major -> {
            major.setUpdateBy(SecurityUtils.getUsername());
            major.setUpdateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).forEach(this::updateByMajorNumberSelective);

    }

    /**
     * 同步删除
     * @param list
     */
    @Transactional
    @Override
    public void syncDeleteMajor(List<Major> list) {
        list.stream().forEach(major -> {
            majorMapper.deleteByMajorNumber(major.getMajorNumber());
        });
    }

    @Override
    public List<Major> convertMajor(Set<MajorImportVo> set) {
        List<Major> majorList = new ArrayList<>();
        set.stream().forEach(m ->{
            //取专业名称hash值生成专业编码
            String majorNumber = String.valueOf(m.getName().hashCode()&Integer.MAX_VALUE);
            String deptNumber = departmentService.findDeptNumberByName(m.getDeptName());
            if (StringUtils.isNotEmpty(deptNumber)){
                Major major = new Major();
                major.setMajorNumber(String.valueOf(majorNumber));
                major.setName(m.getName());
                major.setDeptNumber(deptNumber);
                majorList.add(major);
            }
        });
        return majorList;
    }

    @Override
    public String findMajorNumberByName(String majorName) {
        return majorMapper.findMajorNumberByName(majorName);
    }

    /**
     * 分页查询专业
     * @param majorVo
     * @return
     */
    @Override
    public ResponseResult findMajorPage(MajorVo majorVo) {
        ResponseResult result = new ResponseResult();
        PageHelper.startPage(majorVo.getPageNumber(),majorVo.getPageSize());
        List<MajorVo> majorList = majorMapper.findMajorList(majorVo);
        result.setMsg("分页查询专业成功");
        result.setCode(HttpStatus.SUCCESS);
        result.setData(new PageInfo(majorList));
        return result;
    }

    /**
     * 查询专业列表
     * @param majorVo
     * @return
     */
    @Override
    public ResponseResult findMajorList(MajorVo majorVo) {
        ResponseResult result = new ResponseResult();
        result.setMsg("查询专业成功");
        result.setCode(HttpStatus.SUCCESS);
        result.setData(majorMapper.findMajorList(majorVo));
        return result;
    }

    /**
     * 删除专业
     * @param majorVo
     * @return
     */
    @Transactional
    public ResponseResult deleteMajor(MajorVo majorVo) {
        ResponseResult result = new ResponseResult();
        Major major = selectByPrimaryKey(majorVo.getId());
        if (null != major){
            //专业下有班级不能删
            ClazzVo clazzVo = new ClazzVo();
            clazzVo.setMajorNumber(major.getMajorNumber());

            List<ClazzVo> classList = clazzService.findClassList(clazzVo);
            if (classList.size() > 0){
                result.setCode(HttpStatus.ERROR);
                result.setMsg("不能删除，该专业存在班级");
            }else{
                deleteByPrimaryKey(majorVo.getId());
                result.setMsg("删除专业成功");
                result.setCode(HttpStatus.SUCCESS);
            }
        }else{
            result.setCode(HttpStatus.ERROR);
            result.setMsg("专业不存在，无法删除");
        }

        return result;
    }

    /**
     * 新增专业
     * @param major
     * @return
     */
    @Transactional
    public ResponseResult addMajor(Major major) {

        ResponseResult result = new ResponseResult();
        if (StringUtils.isEmpty(major.getName()) || StringUtils.isEmpty(major.getDeptNumber())) {
            result.setMsg("新增专业失败，缺失专业名称或二级学院名称");
            result.setCode(HttpStatus.ERROR);
            return result;
        }
        String majorName = major.getName();
        //判断专业是否重复
        String majorNumber= findMajorNumberByName(majorName);
        //取专业名称的hash值作为编码
        if(StringUtils.isNotEmpty(majorNumber)){
            result.setMsg("新增失败，专业重复");
            result.setCode(HttpStatus.ERROR);
        }else{
            majorNumber = String.valueOf(majorName.hashCode()&Integer.MAX_VALUE);
            major.setMajorNumber(majorNumber);
            major.setCreateBy(SecurityUtils.getUsername());
            major.setCreateDate(DateUtils.getNowDate());
            insertSelective(major);
            result.setMsg("新增专业成功");
            result.setCode(HttpStatus.SUCCESS);
        }

        return result;
    }
}
